package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.dto.CustomersDto;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.exception.CustomersException;
import com.bridgeintelligent.tag.customer.model.CustomersStopModel;
import com.bridgeintelligent.tag.customer.model.CustomersStopQueryModel;
import com.bridgeintelligent.tag.customer.pojo.Customers;
import com.bridgeintelligent.tag.customer.service.ICustomersStopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @description
 * @author: grp 2021-03-03 21:28
 **/
@Api(tags = "客群失效")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/customersStop")
public class CustomersStopController {

    ICustomersStopService customersStopService;

    @ApiOperation(value = "关联客群列表", notes = "客群停用")
    @GetMapping("list")
    public CustomersDto<CustomersPage<Customers>> list(@ModelAttribute @Valid CustomersStopQueryModel model) {
        model.setPageNumber(model.getPageNumber() == null ? 1 : model.getPageNumber());
        model.setPageSize(model.getPageSize() == null ? 10 : model.getPageSize());
        CustomersPage<Customers> page = customersStopService.list(model);
        return CustomersDto.success(page);
    }

    @ApiOperation(value = "批量/全选", notes = "客群停用")
    @PostMapping("/batch")
    public CustomersDto<Object> batchReplace(@RequestBody CustomersStopModel model) {
        try{
            customersStopService.delete(model);
        }catch (CustomersException e) {
            return CustomersDto.error(e.getMessage());
        }
        return CustomersDto.success();
    }
}
